<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>接口数据测试</title>
    <style>
        body { font-family: Arial; padding: 20px; }
        h2 { color: #333; }
        pre { background: #f5f5f5; padding: 15px; border-radius: 4px; overflow: auto; }
        .success { color: green; }
        .error { color: red; }
        button { padding: 10px 20px; margin: 5px; cursor: pointer; }
    </style>
</head>
<body>
    <h1>🔍 后端接口数据测试</h1>
    
    <button onclick="testPlatformTitle()">1. 测试平台标题</button>
    <button onclick="testBusinessStats()">2. 测试业务统计</button>
    <button onclick="testProjectOverview()">3. 测试项目总览</button>
    <button onclick="testAll()">测试所有接口</button>
    
    <div id="result"></div>
    
    <script>
        const BASE_URL = 'http://localhost:8081';
        
        function showResult(title, data, success = true) {
            const resultDiv = document.getElementById('result');
            const className = success ? 'success' : 'error';
            resultDiv.innerHTML += `
                <h2 class="${className}">${success ? '✅' : '❌'} ${title}</h2>
                <pre>${JSON.stringify(data, null, 2)}</pre>
            `;
        }
        
        async function testPlatformTitle() {
            try {
                const res = await fetch(BASE_URL + '/bigscreen/platformTitle');
                const data = await res.json();
                showResult('平台标题接口', data, data.success);
                
                // 检查数据结构
                if (!data.success) {
                    console.error('❌ success 字段为 false');
                }
                if (!data.data || !data.data.title) {
                    console.error('❌ 缺少 data.title 字段');
                }
            } catch (err) {
                showResult('平台标题接口', { error: err.message }, false);
            }
        }
        
        async function testBusinessStats() {
            try {
                const res = await fetch(BASE_URL + '/bigscreen/countDeviceNum');
                const data = await res.json();
                showResult('业务统计接口', data, data.success);
                
                // 检查必需字段
                const required = ['totalNum', 'orderNum', 'receivableNum', 'paymentNum', 'testPersonNum', 'qcPersonNum', 'qcPassRate'];
                const missing = required.filter(field => !(field in (data.data || {})));
                
                if (missing.length > 0) {
                    console.error('❌ 缺少字段:', missing);
                    showResult('缺少的字段', missing, false);
                }
            } catch (err) {
                showResult('业务统计接口', { error: err.message }, false);
            }
        }
        
        async function testProjectOverview() {
            try {
                const res = await fetch(BASE_URL + '/bigscreen/countUserNum');
                const data = await res.json();
                showResult('项目总览接口', data, data.success);
                
                // 检查必需字段
                const required = ['totalPackageNum', 'completedPackageNum', 'uncompletedPackageNum', 'qcPassPackageNum'];
                const missing = required.filter(field => !(field in (data.data || {})));
                
                if (missing.length > 0) {
                    console.error('❌ 缺少字段:', missing);
                }
            } catch (err) {
                showResult('项目总览接口', { error: err.message }, false);
            }
        }
        
        async function testAll() {
            document.getElementById('result').innerHTML = '<h2>开始测试...</h2>';
            await testPlatformTitle();
            await testBusinessStats();
            await testProjectOverview();
            console.log('✅ 所有测试完成');
        }
        
        // 页面加载时自动测试
        window.onload = () => {
            console.log('🚀 开始自动测试...');
            setTimeout(testAll, 500);
        };
    </script>
</body>
</html>
